﻿using DevExpress.XtraGrid.Views.BandedGrid;
using DLL;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NutritionalRisk
{
    public partial class HistoryNutritionalRisk : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 窗体函数
        public HistoryNutritionalRisk(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            SetBandGridView();
            BindInfo();
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;  //住院号
            int VisitId = this.PatientInfo.VISIT_ID;   //住院次数
            int FormId = (int)FormType.Pabulum;  //表单ID
            DataTable dt = new DataTable();
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.StatDate.DateTime, this.EndDate.DateTime);
            dt.Columns.Add("pingguriqi", typeof(string));
            dt.Columns.Add("zongdefen", typeof(string));
            dt.Columns.Add("tizhong", typeof(string));
            dt.Columns.Add("tizhongzhishu", typeof(string));
            dt.Columns.Add("danbaizhi", typeof(string));
            dt.Columns.Add("hushiqianming", typeof(string));
            dt.Columns.Add("shengao", typeof(string));
            dt.Columns.Add("jibingzhuangtai1", typeof(string));
            dt.Columns.Add("jibingzhuangtai2", typeof(string));
            dt.Columns.Add("jibingzhuangtai3", typeof(string));
            dt.Columns.Add("yiyangzhuangtai1", typeof(string));
            dt.Columns.Add("yiyangzhuangtai2", typeof(string));
            dt.Columns.Add("yiyangzhuangtai3", typeof(string));
            dt.Columns.Add("yiyangzhuangtai4", typeof(string));
            dt.Columns.Add("nianling", typeof(string));
            dt.Columns.Add("chuli1", typeof(string));
            dt.Columns.Add("chuli2", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                string theTrue = "√";
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Nutritions ns = new Nutritions();
                    ns = XmlUtil.XMLToObject<Nutritions>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["pingguriqi"] = Convert.ToDateTime(ns.PingGuRiQi).ToString("yyyy-MM-dd");
                    newRow["zongdefen"] = ns.ZongFen;
                    newRow["tizhong"] = ns.JiChuZiLiao_TiZhong;
                    newRow["tizhongzhishu"] = ns.JiChuZiLiao_TiZhongZhiShu;
                    newRow["danbaizhi"] = ns.JiChuZiLiao_DanBaiZhi;
                    newRow["hushiqianming"] = ns.HuShiQianMing;
                    newRow["shengao"] = ns.ShenGao;
                    #region 疾病状态
                    //疾病状态
                    if (!string.IsNullOrEmpty(ns.JiBingZhuangTai))
                    {
                        string jbzt = ns.JiBingZhuangTai.IndexOf("#") < 0 ? "0#0#0" : ns.JiBingZhuangTai;
                        string[] jbzt_Array = jbzt.Split('#');
                        if (jbzt_Array.Length >= 3)
                        {
                            newRow["jibingzhuangtai1"] = jbzt_Array[0].Equals("1") ? theTrue : string.Empty;
                            newRow["jibingzhuangtai2"] = jbzt_Array[1].Equals("1") ? theTrue : string.Empty;
                            newRow["jibingzhuangtai3"] = jbzt_Array[2].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    #endregion

                    #region 营养状态
                    //营养状态
                    if (!string.IsNullOrEmpty(ns.YingYangZhuangTai))
                    {
                        string yyzt = ns.YingYangZhuangTai.IndexOf("#") < 0 ? "0#0#0#0" : ns.YingYangZhuangTai;
                        string[] yyzt_Array = yyzt.Split('#');
                        if (yyzt_Array.Length >= 4)
                        {
                            newRow["yiyangzhuangtai1"] = yyzt_Array[0].Equals("1") ? theTrue : string.Empty;
                            newRow["yiyangzhuangtai2"] = yyzt_Array[1].Equals("1") ? theTrue : string.Empty;
                            newRow["yiyangzhuangtai3"] = yyzt_Array[2].Equals("1") ? theTrue : string.Empty;
                            newRow["yiyangzhuangtai4"] = yyzt_Array[3].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    #endregion

                    #region 年龄
                    string NiL = ns.NianLing;
                    if (!string.IsNullOrEmpty(NiL))
                    {
                        int n = Convert.ToInt32(NiL);
                        newRow["nianling"] = n == 0 ? "√" : "";
                    }
                    #endregion

                    #region 处理
                    //处理
                    if (!string.IsNullOrEmpty(ns.ChuLi))
                    {
                        string cl = ns.ChuLi.IndexOf("#") < 0 ? "0#0#0#0" : ns.ChuLi;
                        string[] cl_Array = cl.Split('#');
                        if (cl_Array.Length >= 2)
                        {
                            newRow["chuli1"] = cl_Array[0].Equals("1") ? theTrue : string.Empty;
                            newRow["chuli2"] = cl_Array[1].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    #endregion

                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region GridView设置
        //GridView设置
        private void SetBandGridView()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30);
            this.EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.BandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询
        private void btn_Search_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddNutritionalRisk ark = new AddNutritionalRisk(LoginUser, PatientInfo, string.Empty);
            ark.ShowDialog();
            if (ark.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btn_Alter_Click(object sender, EventArgs e)
        {
            if (BandedGridView1.RowCount > 0)
            {
                string Id = BandedGridView1.GetRowCellValue(BandedGridView1.FocusedRowHandle, "Id").ToString();
                AddNutritionalRisk ark = new AddNutritionalRisk(LoginUser, PatientInfo, Id);
                ark.ShowDialog();
                if (ark.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void btn_Print_Click(object sender, EventArgs e)
        {
            PrintNutritionalRisk pr = new PrintNutritionalRisk(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            pr.Show();
        }
        #endregion

        #region 删除
        private void btn_Delete_Click_1(object sender, EventArgs e)
        {
            if (BandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = BandedGridView1.GetRowCellValue(BandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1)
                    {
                        string dateStart = this.StatDate.Text;
                        string dateEnd = this.EndDate.Text;
                        BindInfo();
                    }
                }
            }
        }
        #endregion
    }
}